package cn.lanqiao.blog.service.impl;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lanqiao.blog.dao.ArticleMapper;
import cn.lanqiao.blog.dao.UserMapper;
import cn.lanqiao.blog.dao.ViewMapper;
import cn.lanqiao.blog.entity.User;
import cn.lanqiao.blog.service.UserService;
import cn.lanqiao.blog.vo.UserVO;
import cn.lanqiao.blog.vo.ViewVO;

@Component
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private ArticleMapper articleMapper;
	
	@Autowired
	private ViewMapper viewMapper;

	@Override
	public int addAttention(Integer userId, Integer fansId) {
		// TODO Auto-generated method stub
		int result1 = 0;
		int result2 = 0;
		int result3 = 0;
		result1 = userMapper.addAttention(userId, fansId);
		List<Integer> allArticleId = articleMapper.getAllArticleId(userId);
		for(Integer articleId: allArticleId) {
			result2 = result2 + articleMapper.addFansArticle(fansId, articleId);
		}
		for(Integer articleId: allArticleId) {
			result3 = result3 + articleMapper.addFansCommentable(fansId, articleId);
		}
		if(result1 > 0 && result2 == allArticleId.size() && result3 == allArticleId.size()) {
			return 1;
		}
		return 0;
	}

	@Override
	public int deleteAttention(Integer userId, Integer fansId) {
		// TODO Auto-generated method stub
		int result1 = 0;
		int result2 = 0;
		int result3 = 0;
		result1 = userMapper.deleteAttention(userId, fansId);
		List<Integer> allArticleId = articleMapper.getAllArticleId(userId);
		for(Integer articleId: allArticleId) {
			result2 = result2 + articleMapper.deleteFansArticle(fansId, articleId);
		}
		for(Integer articleId: allArticleId) {
			result3 = result3 + articleMapper.deleteFansCommentable(fansId, articleId);
		}
		if(result1 > 0 && result2 == allArticleId.size() && result3 == allArticleId.size()) {
			return 1;
		}
		return 0;
	}

	@Override
	public int addBlack(Integer userId, Integer blackId) {
		// TODO Auto-generated method stub
		this.deleteAttention(userId, blackId);
		this.deleteAttention(blackId, userId);
		return userMapper.addBlack(userId, blackId);
	}

	@Override
	public int deleteBlack(Integer userId, Integer blackId) {
		// TODO Auto-generated method stub
		return userMapper.deleteBlack(userId, blackId);
	}

	@Override
	public PageInfo<User> getFansList(Integer userId, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userMapper.getFansList(userId);
		PageInfo<User> page = new PageInfo<>(list);
		return page;
	}

	@Override
	public PageInfo<User> getAttentionList(Integer userId, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userMapper.getAttentionList(userId);
		PageInfo<User> page = new PageInfo<>(list);
		return page;
	}

	@Override
	public int getFansCount(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.getFansCount(userId);
	}

	@Override
	public int getAttentionCount(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.getAttentionCount(userId);
	}

	@Override
	public int getCollectionCount(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.getCollectionCount(userId);
	}

	@Override
	public int getOrder(Integer userId) {
		// TODO Auto-generated method stub
		int praiseCount = userMapper.getPraiseCount(userId);
		return userMapper.getOrder(praiseCount);
	}

	@Override
	public List<UserVO> getOrderList() {
		// TODO Auto-generated method stub
		return userMapper.getOrderList();
	}

	@Override
	public User login(String username, String password) {
		// TODO Auto-generated method stub
		return userMapper.login(username, password);
	}

	@Override
	public User getById(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int getBlackCount(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.getBlackCount(userId);
	}

	@Override
	public int getPraiseCount(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.getPraiseCount(userId);
	}

	@Override
	public int getViewerCount(Integer userId) {
		// TODO Auto-generated method stub
		return viewMapper.getViewsCount(userId);
	}

	@Override
	public PageInfo<User> getBlackList(Integer userId, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userMapper.getBlackList(userId);
		PageInfo<User> page = new PageInfo<>(list);
		return page;
	}

	@Override
	public PageInfo<ViewVO> getViewerList(Integer userId, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<ViewVO> list = viewMapper.getViewsList(userId);
		PageInfo<ViewVO> page = new PageInfo<>(list);
		return page;
	}

	@Override
	public int isCollection(Integer userId, Integer articleId) {
		// TODO Auto-generated method stub
		return userMapper.isCollection(userId, articleId);
	}

	@Override
	public int isPraise(Integer userId, Integer articleId) {
		// TODO Auto-generated method stub
		return userMapper.isPraise(userId, articleId);
	}

	@Override
	public int updateUser(User user) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public int addViewer(Integer adminId, Integer viewerId) {
		// TODO Auto-generated method stub
		return userMapper.addViewer(adminId, viewerId);
	}

	@Override
	public User getUserInfo(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(userId);
	}

	@Override
	public int isBlack(Integer viewerId, Integer adminId) {
		// TODO Auto-generated method stub
		return userMapper.isBlack(viewerId, adminId);
	}

	@Override
	public int isAttention(Integer viewerId, Integer adminId) {
		// TODO Auto-generated method stub
		return userMapper.isAttention(viewerId, adminId);
	}

	@Override
	public PageInfo<User> getUserPage(Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<User> page = new PageInfo<>(userMapper.getAll());
		return page;
	}

	@Override
	public int insertUser(User user) {
		// TODO Auto-generated method stub
		return userMapper.insert(user);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteUser(Integer id) {
		// TODO Auto-generated method stub
		userMapper.delete2(id);
		userMapper.delete1(id);
		int result = userMapper.deleteByPrimaryKey1(id);
		if(result == 1) {
			return 1;
		}
		return 0;
	}

	@Override
	public int banUser(User user) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public PageInfo<User> getUserPage2(Integer pageNum, Integer pageSize,User user) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<User> page = new PageInfo<>(userMapper.mohuselect(user));
		return page;
	}
	@Override
	public User adminLogin(String username, String password) {
		// TODO Auto-generated method stub
		return userMapper.login(username, password);
		
	}
	@Override
	public int register(User user) {
		// TODO Auto-generated method stub
		user.setType((byte)0);
		user.setBan(0);
		return userMapper.insert(user);
	}

}
